import React from 'react';
import classNames from 'classnames';
import dayjs from 'dayjs';
import _ from 'lodash';

// 分拆的组件
import CommentCount from './components/CommentCount';
import Tabs from './components/Tabs';
import Forms from './components/Forms';
import CommentList from './components/CommentList';

import './Comments.css'; // 导入样式

export class Comments extends React.Component {
  state = {
    // hot: 热度排序  time: 时间排序
    tabs: [
      {
        id: 1,
        name: '热度',
        type: 'hot',
      },
      {
        id: 2,
        name: '时间',
        type: 'time',
      },
    ],
    active: 'hot',
    list: [
      {
        id: 1,
        author: '刘德华',
        comment: '给我一杯忘情水',
        time: new Date('2021-10-10 09:09:00'),
        // 1: 点赞 0：无态度 -1:踩
        attitude: 1,
      },
      {
        id: 2,
        author: '周杰伦',
        comment: '哎哟，不错哦',
        time: new Date('2021-10-11 09:09:00'),
        // 1: 点赞 0：无态度 -1:踩
        attitude: 0,
      },
      {
        id: 3,
        author: '五月天',
        comment: '不打扰，是我的温柔',
        time: new Date('2021-10-11 10:09:00'),
        // 1: 点赞 0：无态度 -1:踩
        attitude: -1,
      },
    ],
    // 用户输入的评论
    txt: '',
  };

  // tab切换
  changeTab = (type) => {
    console.log(type);
    if (type === 'hot') {
      // id 降序
      const newList = this.state.list.sort((a, b) => {
        // a-b 升序，b-a 降序
        return b.id - a.id;
      });
      // 刷新页面
      this.setState({
        active: type,
        list: newList,
      });
    } else {
      // 时间升序
      // 使用第三方库实现排序
      const newList = _.orderBy(this.state.list, ['time'], ['asc']);
      this.setState({
        active: type,
        list: newList,
      });
    }
  };

  // 删除
  delComment = (id) => {
    console.log(id);
    const newList = this.state.list.filter((item) => item.id !== id);

    // 刷新页面
    this.setState({
      list: newList,
    });
  };
  // 点赞和踩
  changeAttitude = (id, newAttitude) => {
    console.log(id, newAttitude);
    const newList = this.state.list.map((item) => {
      if (item.id === id) {
        return { ...item, attitude: newAttitude };
      } else {
        return item;
      }
    });
    // 刷新页面
    this.setState({
      list: newList,
    });
  };
  // 发表评论
  addComment = (txt) => {
    console.log(txt);
    const comment = {
      id: Date.now(),
      author: '刘德华',
      comment: txt,
      time: new Date(),
      // 1: 点赞 0：无态度 -1:踩
      attitude: 1,
    };
    this.setState({
      list: [comment, ...this.state.list],
    });
  };

  render() {
    return (
      <div className="comment-root">
        <div className="comment-container">
          <CommentCount count={this.state.list.length} />
          <Tabs
            tabs={this.state.tabs}
            active={this.state.active}
            changeTab={this.changeTab}
          />
          <Forms addComment={this.addComment} />
          <CommentList
            list={this.state.list}
            delComment={this.delComment}
            changeAttitude={this.changeAttitude}
          />
        </div>
      </div>
    );
  }
}
